package com.gigi.service.manual;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gigi.entity.LikeShiqinEntity;
import com.gigi.enums.ErrorCodeEnum;
import com.gigi.model.LikeDO;
import com.gigi.model.ListFavoriteRequest;
import com.gigi.service.generated.LikeShiqinService;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class LikeManageService {

    private final LikeShiqinService likeShiqinService;

    public LikeManageService(LikeShiqinService likeShiqinService) {
        this.likeShiqinService = likeShiqinService;
    }

    /**
     * 列表接口
     *
     * @param request
     * @return
     */
    public MultiResponse<LikeShiqinEntity> list(ListFavoriteRequest request) {
        LambdaQueryWrapper<LikeShiqinEntity> wrapper = new LambdaQueryWrapper<LikeShiqinEntity>()
                .eq(LikeShiqinEntity::getUserId, request.getUserId());
        return MultiResponse.ofWithoutTotal(likeShiqinService.list(wrapper));
    }

    /**
     * 新增/删除接口
     *
     * @param request
     * @return
     */
    public Response change(LikeDO request) {
        if (Objects.isNull(request.getId())) {
            LikeShiqinEntity entity = new LikeShiqinEntity();
            entity.setUserId(request.getUserId());
            entity.setBlogId(request.getBlogId());
            if (!likeShiqinService.save(entity)) {
                return Response.buildFailure(ErrorCodeEnum.SAVE_FAILED.getErrCode(), ErrorCodeEnum.SAVE_FAILED.getErrDesc());
            }
        } else {
            if (!likeShiqinService.removeById(request.getId())) {
                return Response.buildFailure(ErrorCodeEnum.DELETE_FAILED.getErrCode(), ErrorCodeEnum.DELETE_FAILED.getErrDesc());
            }
        }
        return Response.buildSuccess();
    }

}
